//*********************************************************************
// Copyright (C) 2014 Hell Prototypes / www.hellprototypes.com
// 
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or (at
// your option) any later version.
//
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
//
//********************************************************************
#include <avr/io.h>
#include <stdio.h>
#include "common.h"

void ui_listmenu(const UI_listmenu_t *p_listmenu)
{
	uint8_t selected_item=0, disp_index=0, disp_line;

	while(1) {
		if(selected_item < 7){//if(selected_item == 0) {
			draw_string(p_listmenu->item_title, true, 4, 0);
			disp_line = 1;
			disp_index = 0;
		} else {
			disp_line = 0;
			disp_index = selected_item - 7;
		}

		for( ; disp_line<8; disp_line++) {
			if(p_listmenu->p_items[disp_index].item_content != NULL) {
				draw_string(p_listmenu->p_items[disp_index].item_content, 
								(disp_index == selected_item) ? true : false, 0, disp_line*8);
				disp_index++;
			} else {
				break;
			}
		}
		draw_end();
		switch( get_key_value(60000) ) {
		case LISTMENU_CONFIRM:
			if(p_listmenu->p_items[selected_item].item_handle != NULL) {
				p_listmenu ->p_items[selected_item].item_handle();
			}
			break;
		case LISTMENU_UP:
			if(selected_item > 0) {
				selected_item--;
			}
			break;
		case LISTMENU_DOWN:
			if(selected_item < p_listmenu->items_num-2) {
				selected_item++;
			}
			break;
		case LISTMENU_CANCLE:
			return;
			break;
		}
	}
}
